qsub <- function(command, tag, q.workDir=NULL,
                 q.binary="y", 
                 q.pe=NULL, q.q="all.q", cmdArgs=NULL,
                 ...){
  dots <- list(...)
  if(is.null(q.workDir)){
    invisible(dir.create(file.path(getwd(), "err", tag),recursive=TRUE,showWarnings=FALSE))
    invisible(dir.create(file.path(getwd(), "log", tag),recursive=TRUE,showWarnings=FALSE))
    if( ('rowBegin'%in%names(dots))&('rowEnd'%in%names(dots)) ){
      q.e=file.path(getwd(), "err", tag, paste(tag,'-',dots$rowBegin,'-',dots$rowEnd,'.err',sep=''))
      q.o=file.path(getwd(), "log", tag, paste(tag,'-',dots$rowBegin,'-',dots$rowEnd,'.out',sep=''))
    }else{
      q.e=file.path(getwd(), "err", tag)
      q.o=file.path(getwd(), "log", tag)
    }
  }else{
    invisible(dir.create(file.path(q.workDir,"err", tag),recursive=TRUE,showWarnings=FALSE))
    invisible(dir.create(file.path(q.workDir,"log", tag),recursive=TRUE,showWarnings=FALSE))
    if( ('rowBegin'%in%names(dots))&('rowEnd'%in%names(dots)) ){    
      q.e=file.path(getwd(), "err", tag, paste(tag,'-',dots$rowBegin,'-',dots$rowEnd,'.err',sep=''))
      q.o=file.path(getwd(), "log", tag, paste(tag,'-',dots$rowBegin,'-',dots$rowEnd,'.out',sep=''))
    }else{
      q.e=file.path(getwd(), "err", tag)
      q.o=file.path(getwd(), "log", tag)
    }
    q.e=file.path(q.workDir, "err", tag)
    q.o=file.path(q.workDir, "log", tag)
  }

  qsubArgs <- list(wd=q.workDir, b=q.binary, 
                   e=q.e, o=q.o, pe=q.pe, q=q.q)
  qsubArgs <- qsubArgs[sapply(qsubArgs, function(a){
    !(is.null(a) | length(a)==0 )  } ) ]
  q.cwd=TRUE
  qsubArgs <- paste("-", names(qsubArgs), " ", qsubArgs , sep="")
  if(is.null(q.workDir)) qsubArgs <- c(qsubArgs, "-cwd")
  qsubArgs <- c(qsubArgs, paste("-N", tag))
  
  system(paste("qsub", 
               paste(qsubArgs, collapse=" "),
               paste("\""),
               command, 
               paste(cmdArgs, collapse=" "),
               paste("\"")
               ))
}
